/* 事件上报 */
export const Log = {
  version: 'v2.0.0',
  getCmsConfigErr: 'GET_CMSCONFIG_ERROR',
  requestFail: 'REQUEST_FAIL',
  requestCatch: 'REQUEST_CATCH',
  sendSmsCodeErr: 'SEND_SMSCODE_ERROR',
  verifySmsCodeErr: 'VERIFY_SMSCODE_ERROR',
  updateIdinfoErr:'UPDATE_IDINFI_ERROR',
  checkIdinfoErr:'CHECK_IDINFO_ERROR',
  ocrinfoErr: 'OCR_INFO_ERROR',
  ocrinfoUploadErr: 'OCR_INFO_UPLOAD_ERROR',
  ocrinfoUploadFail:'OCR_INFO_UPLOAD_FAIL',
  preLivingbodyExecErr: 'PRE_LIVINGBODY_EXEC_ERROR',
  livingbodyVerifyErr: 'LIVINGBODY_VERIFY_ERROR',
  exitVerifyFail:'EXIT_VERIFY_FAIL',
  lipcodeErr: 'LIP_CODE_ERROR',
  memoryWarn:'MEMORY_WARNING'
};

// 判断是在正式环境还是在开发体验环境
function isRelease() {
  // envVersion 为空或release 并且 不是debug模式
  return (!__wxConfig.envVersion || __wxConfig.envVersion === 'release')
    && !__wxConfig.debug;
}
const getNetWorkData = () => {
  return new Promise((resolve, reject) => {
    try {
      uni.getNetworkType({
        success(res) {
          resolve(res);
        },
        fail(err) {
          console.log(`requestPromise error:`, err);
          reject({ ErrorCode: 101, ErrorMsg: err.errMsg });
        },
      });
    } catch (e) {
      console.log('捕获error');
    }
  });
};
export async function reportError(tag, error, extra) {
  if (!uni.faceidVerifySdkToken || !isRelease()) return // 非正式环境不进行上报

  const errorData = {
    tag: Log[tag],
    error: { version: Log.version, signalStrength: null, ...error },
    source: 'miniprogram',
  };
  try {
    errorData.system = uni.getSystemInfoSync();
    errorData.error.signalStrength = await getNetWorkData();
  } catch (e) { }
  if (extra) {
    errorData.extra = extra;
  }
  console.log('上报错误：', errorData);
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${uni.faceidVerifySdkBaseUrl}/api/report/reportError`,
      method: 'POST',
      data: {
        token: token,
        errorData: JSON.stringify(errorData),
      },
      success(res) {
        resolve(res);
      },
      fail(err) {
        reject(err);
      },
    });
  });
};


// 上报接口请求
export async function reportMonitor(token, tag, code) {
  if (!isRelease()) return; // 非正式环境不进行上报
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${uni.faceidVerifySdkBaseUrl}/api/report/reportMonitor`,
      method: 'POST',
      data: {
        token: token,
        pathName: tag,
        platForm: 2,
        errorCode: String(code),
        version: Log.version
      },
      success(res) {
        resolve(res);
      },
      fail(err) {
        reject(err);
      },
    });
  });
};